﻿using System;
using System.Windows.Data;

namespace Hibiku.ValueConverters
{
    [ValueConversion(typeof(DateTime), typeof(string))]
    public class DateTimeToTimeAgoConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var dateTime = System.Convert.ToDateTime(value);

            var timeSpan = DateTime.Now.Subtract(dateTime);

            if (timeSpan.TotalMinutes < 2)
            {
                return "just now";
            }
            else if (timeSpan.TotalHours < 1.5)
            {
                return string.Format("{0}m ago", timeSpan.Minutes);
            }
            else if (timeSpan.TotalDays < 1.5)
            {
                return string.Format("{0}h ago", timeSpan.Hours);
            }
            else if (timeSpan.TotalDays < 7)
            {
                return string.Format("{0}d ago", timeSpan.Days);
            }
            else
            {
                return dateTime.ToString("YYYY-mm-dd");
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
